package com.ye.personnel.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ye.personnel.api.department.GwyPullDepartmentListVo;
import com.ye.personnel.api.enums.AuthorizedTypeEnum;
import com.ye.personnel.api.enums.ManageTypeEnum;
import com.ye.personnel.api.enums.MemberTypeEnum;
import com.ye.personnel.api.flow.PersonnelDetailVo;
import com.ye.personnel.api.flow.PersonnelFamilyListVo;
import com.ye.personnel.api.flow.PersonnelHeaderPortraitVo;
import com.ye.personnel.api.flow.PersonnelSalaryDetailVo;
import com.ye.personnel.api.gwy.GwyMemberListVo;
import com.ye.personnel.api.gwy.MemberListByDepartmentNoSearch;
import com.ye.personnel.common.page.PageModel;
import com.ye.personnel.mapper.dao.GwyDepartmentMapper;
import com.ye.personnel.mapper.dao.GwyMapper;
import com.ye.personnel.mapper.entity.GwyDepartment;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yeubo
 * @Date: 2024/1/3 10:20
 */
@Service
@Slf4j
@DS("slave_1")
public class GwyCmsSystemService {

    @Autowired
    GwyMapper gwyMapper;
    @Autowired
    GwyDepartmentMapper departmentMapper;

    @Value("${upload.headerPortrait}")
    private String headerPortrait;


    /**
     * 获取某个单位下的人员列表
     * @param departmentNo
     * @return
     */
    public List<GwyMemberListVo> getMemberList(String departmentNo){
        return gwyMapper.memberList(departmentNo);
    }

    /**
     * 拉取单位
     * @return
     */
    public List<GwyPullDepartmentListVo> pullDepartmentList(){
        return gwyMapper.pullDepartmentList();
    }

    /**
     * 人员任免表
     * @param personnelNo
     * @return
     */
    public PersonnelDetailVo personnelDetail(String personnelNo){
        //基本信息
        PersonnelDetailVo personnelDetailVo = gwyMapper.personnelDetail(personnelNo);
        String manageCategoryName = ManageTypeEnum.getName(personnelDetailVo.getManageCategory());
        personnelDetailVo.setManageCategoryName(manageCategoryName);

        String memberCategoryName = MemberTypeEnum.getName(personnelDetailVo.getMemberCategory());
        personnelDetailVo.setMemberCategoryName(memberCategoryName);

        String authorizedCategoryName = AuthorizedTypeEnum.getName(personnelDetailVo.getAuthorizedCategory());
        personnelDetailVo.setAuthorizedCategoryName(authorizedCategoryName);
        //家庭成员
        List<PersonnelFamilyListVo> personnelFamilyListVos = gwyMapper.personnelFamilyList(personnelNo);
        personnelDetailVo.setPersonnelFamilyList(personnelFamilyListVos);
        //头像
        PersonnelHeaderPortraitVo headerPortraitVo = gwyMapper.personnelHeaderPortrait(personnelNo);
        if(headerPortraitVo != null && headerPortraitVo.getStatus() == 1){
            String p = headerPortrait + "/HZBPHOTOS/" + headerPortraitVo.getPhotoPath() + headerPortraitVo.getPhotoName();
            personnelDetailVo.setHeadPortrait(p);
        }
        //职位类别
        if(StringUtils.isNotBlank(personnelDetailVo.getPositionCategory())){
            String positionCategoryName = gwyMapper.getCodeName("XZ93", "A0123", personnelDetailVo.getPositionCategory());
            personnelDetailVo.setPositionCategoryName(positionCategoryName);
        }
        //现职级
        if(StringUtils.isNotBlank(personnelDetailVo.getPositionRank())){
            String positionRankName = gwyMapper.getCodeName("ZB148", "A0192E", personnelDetailVo.getPositionRank());
            personnelDetailVo.setPositionRankName(positionRankName);
        }
        //级别
        if(StringUtils.isNotBlank(personnelDetailVo.getRankStr())){
            String rankName = gwyMapper.getCodeName("ZB134", "A0120", personnelDetailVo.getRankStr());
            personnelDetailVo.setRankStr(rankName);
        }
        //民族
        if(StringUtils.isNotBlank(personnelDetailVo.getNationName())){
            String nationName = gwyMapper.getCodeName("GB3304", "A0117", personnelDetailVo.getNationName());
            personnelDetailVo.setNationName(nationName);
        }
/*        //简历
        if(StringUtils.isNotBlank(personnelDetailVo.getCurriculum())){
            String replace = personnelDetailVo.getCurriculum().replace(" ", "\n");
            personnelDetailVo.setCurriculum(replace);
        }*/
        //单位名称
        String relationOrganizationName = gwyMapper.departmentNameByNo(personnelDetailVo.getRelationOrganizationNo());
        personnelDetailVo.setRelationOrganizationName(relationOrganizationName);
        //工资信息
        PersonnelSalaryDetailVo personnelSalaryDetailVo = gwyMapper.personnelSalaryDetail(personnelNo);
        if(personnelSalaryDetailVo != null){
            String startWorkTime = personnelDetailVo.getStartWorkTime();
            String startWorkYear = StringUtils.substring(startWorkTime, 0, 4);
            int startWorkYearInt = Integer.parseInt(startWorkYear);
            int thisYear = DateUtil.year(new Date());
            personnelSalaryDetailVo.setWorkYears(thisYear-startWorkYearInt);
            personnelDetailVo.setPersonnelSalaryDetailVo(personnelSalaryDetailVo);
        }
        return personnelDetailVo;
    }

    /**
     * 获取各个职级的实职人数
     * @param departmentNo
     * @param type
     * @return
     */
    public Integer getDepartmentPositionRealNum(String departmentNo,String type){
        return gwyMapper.getDepartmentPositionRealNum(departmentNo,type);
    }


    public Integer getDepartmentPositionRealNumSec(String departmentNo,String codeValue,String codeValueSec){
        return gwyMapper.getDepartmentPositionRealNumSec(departmentNo,codeValue,codeValueSec);
    }

    /**
     * 人员列表
     * @param search
     * @return
     */
    public PageModel<GwyMemberListVo> memberListByDepartmentNo(MemberListByDepartmentNoSearch search){
        Page<GwyMemberListVo> rowPage = new Page<>(search.getPage(), search.getPageSize());
        String type = search.getType();
        String codeValue = "";
        String codeValueSec = "";
        if (Objects.equals(type, "zk")) {
            codeValue = "23";
        }else if( Objects.equals(type, "fk")){
            codeValue = "24";
        }else if(Objects.equals(type, "ky")){
            if(type.equals("ky")){
                codeValue = "17";
                codeValueSec = "18";
            }
        }else if(Objects.equals(type, "kySec")){
            codeValue = "19";
            codeValueSec = "1A";
        }
        search.setCodeValue(codeValue);
        search.setCodeValueSec(codeValueSec);
        Page<GwyMemberListVo> gwyMemberListVoPage = gwyMapper.memberListByDepartmentNo(rowPage, search);
        List<GwyMemberListVo> records = gwyMemberListVoPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageModel<>(gwyMemberListVoPage, Collections.emptyList());
        }
        for (GwyMemberListVo memberListVo : records){
            String positionRankName = gwyMapper.getCodeName("ZB148", "A0192E", memberListVo.getPositionRank());
            memberListVo.setPositionRank(positionRankName);
        }
        return new PageModel<>(gwyMemberListVoPage,records);
    }
}
